package com.ruoyi.system.controller;

import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.service.IGoodsService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("minio")
@CrossOrigin("*")
public class MinioController {
    @Autowired
    IGoodsService iGoodsService;

    @SneakyThrows
    @RequestMapping("minioAdd")
    public String minioAdd(@RequestParam MultipartFile file,@RequestParam String name){
        MinioClient minioClient= MinioClient.builder()
                .endpoint("http://47.111.128.72:19000")
                .credentials("minioadmin","minioadmin")
                .build();
        String string = UUID.randomUUID().toString();

        PutObjectArgs putObjectArg=PutObjectArgs.builder()
                .contentType("image/png")
                .object(string+file.getOriginalFilename())
                .bucket("zhentao-project")
                .stream(file.getInputStream(), file.getSize(), -1)
                .build();
        minioClient.putObject(putObjectArg);

        Goods goods=new Goods();
        goods.setImages(minioClient.getObjectUrl(putObjectArg.bucket(), putObjectArg.object()));
        goods.setGoodsName(name);
//        iGoodsService.insertGoods(goods);
        return goods.getImages();
    }
}
